﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Eaf.Net.AccessControl.Entity.User;
using Eaf.Net.AccessControl.BusinessManager.User;
using Eaf.Net.AccessControl.DataAccess.User;
using System.Transactions;
using Eaf.Net.AccessControl.DataAccess.Role;

namespace Hfm.Core.BusinessManager.User
{
    public class AMUserManager : UserManager<UserAccountEntity>
    {
        protected readonly RoleData<UserAccountEntity> RoleDataAccess;

        public AMUserManager(UserData<UserAccountEntity> dataAccess, RoleData<UserAccountEntity> rDataAccess)
            : base(dataAccess)
        {
            RoleDataAccess = rDataAccess;
        }

        public virtual void CreateUser(UserAccountEntity newUser,int roleId, bool sendActiveEmail = false)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                base.CreateUser(newUser, sendActiveEmail);
                if (roleId > 0)
                {
                    RoleDataAccess.AddUserToRole(newUser.UserId, roleId);
                }
                scope.Complete();
            }
        }

        public virtual void UpdateUser(UserAccountEntity existingUser, int roleId, bool isModifyPassword = false)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                base.UpdateUser(existingUser, isModifyPassword);
                if (roleId > 0)
                {
                    RoleDataAccess.DeleteUserRoles(existingUser.UserId);
                    RoleDataAccess.AddUserToRole(existingUser.UserId, roleId);
                }
                scope.Complete();
            }
        }
    }
}
